package com.ruoyi.hazard.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hazard.domain.HazardAlarmRecord;
import com.ruoyi.hazard.domain.HazardInfo;
import com.ruoyi.hazard.domain.qo.HazardAlarmRecordQo;
import com.ruoyi.hazard.mapper.HazardAlarmRecordMapper;
import com.ruoyi.hazard.service.HazardAlarmRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class HazardAlarmRecordServiceImpl extends ServiceImpl<HazardAlarmRecordMapper, HazardAlarmRecord> implements HazardAlarmRecordService {
    @Override
    public Wrapper<HazardAlarmRecord> getQueryWrapper(HazardAlarmRecordQo qo) {
        LambdaQueryWrapper<HazardAlarmRecord> wrapper = new LambdaQueryWrapper<>();
        //序号
        if (qo.getId() != null) {
            wrapper.eq(HazardAlarmRecord::getId, qo.getId());
        }
        // 通知状态
        if(qo.getHandled()!=null){
            wrapper.eq(HazardAlarmRecord::getHandled, qo.getHandled());
        }
        // 通知类型
        if(StringUtils.hasText(qo.getType())){
            wrapper.eq(HazardAlarmRecord::getType, qo.getType());
        }
        // 接收时间
        if(qo.getReceiveTimeStart() != null){
            wrapper.ge(HazardAlarmRecord::getReceiveTime, qo.getReceiveTimeStart());
        }
        if(qo.getReceiveTimeEnd() != null){
            wrapper.le(HazardAlarmRecord::getReceiveTime, qo.getReceiveTimeEnd());
        }
        // 处理时间
        if(qo.getHandTimeStart() != null){
            wrapper.ge(HazardAlarmRecord::getHandleTime, qo.getHandTimeStart());
        }

        if(qo.getHandleTimeEnd() != null){
            wrapper.le(HazardAlarmRecord::getHandleTime, qo.getHandleTimeEnd());
        }
        // 隐患ID
        if(StringUtils.hasText(qo.getHazardId())){
            wrapper.like(HazardAlarmRecord::getHazardIds, qo.getHazardId());
        }
        //如果不是超级管理员 就查本人的
        if(!StringUtils.equals("admin", SecurityUtils.getLoginUser().getUsername())){
            wrapper.eq(HazardAlarmRecord::getReceiveBy, SecurityUtils.getLoginUser().getUsername());
        }
        wrapper.orderByAsc(HazardAlarmRecord::getHandled);
        wrapper.orderByDesc(HazardAlarmRecord::getId);
        return wrapper;
    }
}
